package com.exam.aopsolution;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Aspect
@Component
@Order(1) //---------------------------------定义优先级--------------------------

public class TimeKeepingAspect {
static final StopWatch stopWatch = new StopWatch("Response time calculation");//静态StopWatch实例
@Pointcut("@annotation(com.exam.aopsolution.TimeKeeping)")//定义切点，匹配所有标记有@TimeKeeping注解的方法
public void pointcut() { }
@Around("pointcut()")
public Object timing(ProceedingJoinPoint pjp) throws Throwable {//环绕通信方法，用于计算方法执行时间
         Object result;
         stopWatch.start(pjp.getSignature().toShortString());
         System.out.println("计时开始");
         try {
            result = pjp.proceed();
         } finally {
            stopWatch.stop();
             System.out.println(stopWatch.prettyPrint());
         }
 return result;
 }
}